<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="diaShow">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="rolesList" border>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              :class="['borderB',i1==0?'borderT':'']"
              v-for="(item1,i1) in scope.row.children"
              :key="item1.id"
              type="flex"
              align="moddle"
            >
              <el-col :span="5">
                <el-tag closable @close="deleteById(scope.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row
                  :class="[i2==0?'':'borderT']"
                  v-for="(item2,i2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      closable
                      @close="deleteById(scope.row,item2.id)"
                      type="success"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      closable
                      @close="deleteById(scope.row,item3.id)"
                      type="warning"
                      :class="['borderB',i3==0?'borderT':'']"
                      v-for="(item3,i3) in item2.children"
                      :key="item3.id"
                    >{{item3.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="角色名称" center prop="roleName"></el-table-column>
        <el-table-column label="角色描述" center prop="roleDesc"></el-table-column>
        <el-table-column label="等级">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="diaShowsss(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="deletejs(scope.row.id)"
            >删除</el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="fenpeiqx(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <QuanxianDialog :quanxianShow="quanxianDialog" @quanxianClose="CloseS" :node="nodes" @undates="this.getrolesList"></QuanxianDialog>
    <DiaLog :showDig="showDia" @aaa="closeDia" @update="updata"></DiaLog>
    <EtimJuese :showDig="showDiass" @aaa="closeDiass" @update="updata" :id="ids"></EtimJuese>
  </div>
</template>

<script>
import Dialog from "../components/AddjueseLog";
import EtimJuese from "../components/etimjuese";
import QuanxianDialog from "../components/quanxianDialog";
export default {
  data() {
    return {
      showDia: "",
      showDiass: "",
      rolesList: [],
      ids: "",
      quanxianDialog: false,
      nodes:''
    };
  },
  components: {
    DiaLog: Dialog,
    EtimJuese,
    QuanxianDialog,
  },
  created() {
    this.getrolesList();
  },
  methods: {
    //添加用户
    diaShow() {
      this.showDia = true;
    },
    closeDia() {
      this.showDia = false;
    },
    closeDiass() {
      this.showDiass = false;
    },
    diaShowsss(id) {
      this.ids = id;

      this.showDiass = true;
    },
    CloseS() {
      this.quanxianDialog = false;
    },
    //获取列表数据
    getrolesList() {
      this.$axios.get("roles").then((res) => {
        console.log(res);
        if (res.data.meta.status !== 200) {
          return this.$message.error("获取角色列表数据失败！");
        }
        this.rolesList = res.data.data;
        console.log(this.rolesList);
      });
    },
    //刷新
    updata() {
      this.getrolesList();
      this.showDia = false;
      console.log("sss");
    },
    //删除角色
    deletejs(id) {
      this.$axios.delete(`roles/${id}`).then((res) => {
        if (res.data.meta.status !== 200) {
          return this.$message.error("删除失败！");
        }
        this.$message({
          message: res.data.meta.msg,
          type: "success",
        });
        this.getrolesList();
      });
    },
    //删除权限
    deleteById(row, ids) {
      console.log(row.id + "--" + ids);
      this.$messageBox("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          this.$axios.delete(`roles/${row.id}/rights/${ids}`).then((res) => {
            if (res.data.meta.status == 200) {
              this.$message({
                type: "success",
                message: res.data.meta.msg,
              });
              row.children = res.data.data;
              // this.getrolesList();
            } else {
              this.$message({
                type: "error",
                message: "取消权限失败!",
              });
            }
            console.log(res);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分配权限
    fenpeiqx(node) {
      this.nodes=node;
      console.log( this.nodes)
      this.quanxianDialog = true;
    },
  },
};
</script>
<style  scoped>
.el-tag {
  margin: 7px;
}
.borderT {
  border-top: 1px solid #ccc;
}
.borderB {
  border-bottom: 1px solid #ccc;
}
.el-row {
  display: flex;
  align-items: center;
}
</style>